import java.sql.*;
public class RunDB {
	Connection connection;
	   Statement statement;

	   public void loadDriver() throws ClassNotFoundException{
	      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
	   }

	   public void makeConnection() throws SQLException {
	      connection=
	         DriverManager.getConnection("jdbc:odbc:student");
	   }

	   public void buildStatement() throws SQLException {
	      statement = connection.createStatement();
	   }

	   public void executeQuery(String query) throws SQLException {

	      boolean foundResults =
	         statement.execute(query);
	      System.out.println(query);
	      if(foundResults){
	         ResultSet set = statement.getResultSet();
	         if(set!=null) displayResults(set);
	      }else {
	         connection.close();
	      }
	   }

	   void displayResults(ResultSet rs) throws SQLException {
	      ResultSetMetaData metaData = rs.getMetaData();
	      int columns=metaData.getColumnCount();
	      String text="";

	      while(rs.next()){
	         for(int i=1;i<=columns;++i) {
	        	// System.out.println(rs.getString(i));
	            text+="<"+metaData.getColumnName(i)+">";
	            text+=rs.getString(i);
	             text+="</"+metaData.getColumnName(i)+">";
	             text+="\n";
	         }
	         text+="\n";
	      }
	      //System.out.println("these are the results");
	      System.out.println(text);
	      //System.out.println("printed");
	   }

}
